﻿using AutoMapper;
using BlogSystem.Common.Config;
using BlogSystem.Common.Config.DI.annotation;
using BlogSystem.Data.Entity;
using BlogSystem.Exception;
using BlogSystem.Model.Request;
using BlogSystem.Model.Respones;
using Microsoft.EntityFrameworkCore;

namespace BlogSystem.Data.Repository.RepositoryImpl;

[Ioc("scoped")]
public class ArticleRepository : BaseRepository<Article>,IArticleRepository
{
    private readonly DataBaseContext _dbContext;
    
    private readonly IMapper _mapper;

    public ArticleRepository(DataBaseContext databaseContext,IMapper mapper) : base(databaseContext)
    {     
        _mapper = mapper;
        _dbContext = databaseContext;
    }

    public async Task<IQueryable<ArticleResponse>> GetAllByPage(string keyWord, int pageIndex, int pageSize)
    {
        var articlesPage = _dbContext.Set<Article>()
            .Where(a => a.Status == 1 && (a.Content.Contains(keyWord) || a.Title.Contains(keyWord)))
            .OrderBy(b => b.CreateTime)
            .Skip((pageIndex - 1) * pageSize)
            .Take(pageSize)
            .Select(e => _mapper.Map<ArticleResponse>(e));
     
       return articlesPage;
    }


    public async Task<bool> AddLikeById(int id)
    {
        // 1、根据id 查找，是否 存在 该文章
       var article = await _dbContext.Set<Article>()
           .FirstOrDefaultAsync(a => a.Status == 1 && a.Id == id);
        if (article == null)
            throw new BusinessException("点赞有误。");
        else
        {
            // 2、有【获取到该条数据 再进行添加】 
            // 从list当中拿到第一条数据
            article.LikeCount++;
            await _dbContext.SaveChangesAsync();
            return true;
        }
    }

    public async Task<bool> AddSaveById(int id)
    {
        // 1、根据id 查找，是否 存在 该文章
        var article = await _dbContext.Set<Article>()
            .FirstOrDefaultAsync(a => a.Status == 1 && a.Id == id);

        if (article == null)
            throw new BusinessException("收藏有误。");
        else
        {
            // 2、有【获取到该条数据 再进行添加】 
            // 从list当中拿到第一条数据
            article.SaveCount++;
            await _dbContext.SaveChangesAsync();
            return true;
        }
    }

    public async Task<bool> AddReadById(int id)
    {
        // 1、根据id 查找，是否 存在 该文章
        var article = await _dbContext.Set<Article>()
            .FirstOrDefaultAsync(a => a.Status == 1 && a.Id == id);

        if (article == null)
            throw new BusinessException("点赞有误。");
        else
        {
            // 2、有【获取到该条数据 再进行添加】 
            // 从list当中拿到第一条数据
            article.ReadCount++;
            await _dbContext.SaveChangesAsync();
            return true;
        }
    }

    public async Task<bool> DelArticleById(int id)
    {
        var article = await _dbContext.Set<Article>()
            .FirstOrDefaultAsync(a => a.Status == 1 && a.Id == id);

        if (article==null)
        {
            throw new BusinessException("信息存在，请重新输入");
        }
        else
        {
            article.Status = 0;
            await _dbContext.SaveChangesAsync();
            return true;
        }
    }
}